package com.liao.myblog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.liao.myblog.enums.ResultCode;
import com.liao.myblog.pojo.Comment;
import com.liao.myblog.service.CommentService;
import com.liao.myblog.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/admin")
@Controller
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping(value = "/comments")
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "comments");
        return "/admin/comment";
    }

    //通过blogId查询对应的评论
    @ResponseBody
    @PostMapping(value = "/comment")
    public List<Comment> queryCommentById(int id) {
        return commentService.queryCommentByBlogId(id);
    }

    @ResponseBody
    @GetMapping(value = "/comment/list")
    public PageInfo<Comment> queryCommentAll(@RequestParam("rows")int rows,
                                             @RequestParam("page")int page,
                                             @RequestParam("sord")String sord) {
        return commentService.queryCommentList(rows,page);
    }

    @ResponseBody
    @PostMapping(value = "/comment/delete")
    public ResultVO<Object> deleteComment(@RequestBody int[] ids) {
        int i = commentService.deleteComment(ids);
        if (i > 0) {
            return new ResultVO<>(ResultCode.SUCCESS);
//            return ResultUtil.getResult("删除成功", ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult("删除失败", ResultUtil.RESULT_ERROR);
        }
    }

    @ResponseBody
    @PostMapping(value = "/comment/check")
    public ResultVO<Object> checkComment(@RequestBody int[] ids) {
        System.out.println(Arrays.toString(ids));
        int i = commentService.checkComment(ids);
        if (i > 0) {
            return new ResultVO<>(ResultCode.SUCCESS);
//            return ResultUtil.getResult("审核成功", ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult("审核失败", ResultUtil.RESULT_ERROR);
        }
    }

    @ResponseBody
    @PostMapping(value = "/comment/reply")
    public ResultVO<Object> replyComment(@RequestParam(value = "content") String content,
                                         @RequestParam(value = "blog_id") int blogId,
                                         @RequestParam(value = "email") String email) {
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setIsDelete((byte) 0);
        comment.setCommentStatus((byte) 0);
        comment.setTime(new Date());
        comment.setBlogId(blogId);
        comment.setEmail(email);
        comment.setUsername("admin");
        int i = commentService.replyComment(comment);
        if (i > 0) {
            return new ResultVO<>(ResultCode.SUCCESS);
//            return ResultUtil.getResult("回复成功", ResultUtil.RESULT_SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.ERROR);
//            return ResultUtil.getResult("回复失败", ResultUtil.RESULT_SUCCESS);
        }
    }

}